package io.light.frame.dal.mybatis.generator.core.mapper.func.injector;

import io.light.frame.dal.mybatis.generator.core.domain.mapper.MapperFunc;
import io.light.frame.dal.mybatis.generator.core.mapper.func.FuncBuilder;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;

/**
 * Function injector
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-06-12 11:38
 */
public abstract class FuncInjector implements FuncBuilder {

    /**
     * Worked element name
     *
     * @return element name
     * @see Element#getName()
     */
    public abstract String workedElement();

    /**
     * Accept this function`s element or not
     *
     * @param element    function element
     * @param mapperFunc {@link MapperFunc}
     * @return {@code boolean} accept or not
     */
    @Override
    public final boolean accept(Element element, MapperFunc mapperFunc) {
        if (mapperFunc.isAutoGen()) {
            return false;
        }
        if (StringUtils.isBlank(workedElement())) {
            return false;
        }
        if (MapperFunc.Type.of(element) != null) {
            return false;
        }
        if(!workedElement().equalsIgnoreCase(element.getName())){
            return false;
        }
        return canAccept(element, mapperFunc);
    }

    /**
     * Accept this function`s element or not
     *
     * @param element    function element
     * @param mapperFunc {@link MapperFunc}
     * @return {@code boolean} accept or not
     */
    protected boolean canAccept(Element element, MapperFunc mapperFunc) {
        return true;
    }
}
